package com.cn.service;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.Cluster;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.cn.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

/**
 * @author: chenyuncong
 * @Date: 2019/12/5 9:26
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserServiceImpl implements UserService {

//    @Autowired
//    private NamingService namingService;

    @Autowired
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    @Value("${spring.cloud.nacos.discovery.server-addr}")
    private String serverAddr;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("getDataMete")
    public  Map<String,String> getDataMete() throws NacosException {

        String dataId = "service-provider";
//        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        ConfigService configService = NacosFactory.createConfigService(properties);

        NamingService naming = NamingFactory.createNamingService(serverAddr);
        List<Instance> list = naming.getAllInstances(dataId);
        list.forEach(x->{
            try {
                x.getMetadata().put("test","test数据：{"+ UUID.randomUUID().toString()+"}");
                naming.registerInstance(dataId,x);
            } catch (NacosException e) {
                e.printStackTrace();
            }
        });
        System.out.println(naming.getAllInstances(dataId));


        Map<String,String> map = nacosDiscoveryProperties.getMetadata();
        return map;
    }

    @RequestMapping("setDataMete")
    public  List<Instance> setDataMete() throws NacosException {
        NamingService naming = NamingFactory.createNamingService(serverAddr);
        List<Instance> list = naming.getAllInstances(null);

//        Map<String,String> map = nacosDiscoveryProperties.getMetadata();
//        map.put("test01","val-01");
//        map.put("test02","val-02");
        return list;
    }

    String ip=null;
    public UserServiceImpl(){
        try {
            ip =InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User getUserById(Integer id) {
        Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                log.info("name:[{}] valuse:[{}]",name,values);
            }
        return new User(id,"小明"+id,"男",ip,"");
    }

    @Override
    public User getUserByName(String name) {
        return new User(2,name,"男", ip,"");
    }
}
